var time = 0;

var timer = setInterval(() => {
  time++
  if (time > 5) {
    clearInterval(timer)
  }
  console.log(time);
}, 1000);